from module.static_module.parent.view import SubWindowView
from tools.view import *


class KChartView(SubWindowView):
    def __init__(self, main_window_view):
        super().__init__(main_window_view)
        self.class_name = Module.KChart.value
        tk.Label(self.frame, text=self.class_name).pack()

    def create_layout_init(self):
        # 绘制标签、内容组件
        # 配置参数
        model_name = self.main_window_view.master.file_manager.now_model_name
        run_mode_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].run_mode_ls

        data_name_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].data_name_ls
        symbol_name_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].symbol_name_ls
        k_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].k_factor_ls
        vol_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].vol_factor_ls
        factor_1_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].factor_1_ls
        factor_2_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].factor_2_ls

        start_time_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].start_time_ls
        end_time_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].end_time_ls

        label_config = [
            [LabelMember.ModelName, WidgetCategory.Label, [model_name], [0]],
            [LabelMember.RunMode, WidgetCategory.Combobox, run_mode_ls, []],
            [LabelMember.DataName, WidgetCategory.Combobox, data_name_ls, []],
            [LabelMember.SymbolName, WidgetCategory.Combobox, symbol_name_ls, []],
            [LabelMember.KFactor, WidgetCategory.CheckButton, k_factor_ls, []],
            [LabelMember.VolFactor, WidgetCategory.CheckButton, vol_factor_ls, []],
            [LabelMember.Factor1, WidgetCategory.CheckButton, factor_1_ls, []],
            [LabelMember.Factor2, WidgetCategory.CheckButton, factor_2_ls, []],
            [LabelMember.StartTime, WidgetCategory.Entry, start_time_ls, []],
            [LabelMember.EndTime, WidgetCategory.Entry, end_time_ls, []]
        ]
        button_config = [
            [ButtonMember.Ok, WidgetCategory.Button, None],
            [ButtonMember.Stop, WidgetCategory.Button, None]
        ]
        self.auto_layout = AutoLayout(self.frame, label_config, button_config)
        self.auto_layout.button_command(self)
        bind_change_behavior = [
            [LabelMember.RunMode, self.on_run_mode_change],
            [LabelMember.DataName, self.on_data_name_change],
            [LabelMember.SymbolName, self.on_symbol_name_change]
        ]
        self.auto_layout.label_command(bind_change_behavior)

    def update_layout(self):

        pass

    def on_run_mode_change(self):
        # 由实体类加载相应属性
        self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].on_run_mode_change()
        if not self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].run_mode:
            return
        # 视图类方法映射到实体类的方法
        # 配置参数
        # 历史数据布局
        data_name_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].data_name_ls
        symbol_name_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].symbol_name_ls
        k_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].k_factor_ls
        vol_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].vol_factor_ls
        factor_1_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].factor_1_ls
        factor_2_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].factor_2_ls

        start_time_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].start_time_ls
        end_time_ls = self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].end_time_ls

        label_config = [
            [LabelMember.DataName, WidgetCategory.Combobox, data_name_ls, []],
            [LabelMember.SymbolName, WidgetCategory.Combobox, symbol_name_ls, []],
            [LabelMember.KFactor, WidgetCategory.CheckButton, k_factor_ls, []],
            [LabelMember.VolFactor, WidgetCategory.CheckButton, vol_factor_ls, []],
            [LabelMember.Factor1, WidgetCategory.CheckButton, factor_1_ls, []],
            [LabelMember.Factor2, WidgetCategory.CheckButton, factor_2_ls, []],
            [LabelMember.StartTime, WidgetCategory.Entry, start_time_ls, []],
            [LabelMember.EndTime, WidgetCategory.Entry, end_time_ls, []]
        ]

        self.auto_layout.update_label_layout(label_config)

    def on_data_name_change(self):
        # 由实体类加载相应属性
        self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].on_data_name_change()
        if not self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].data_name:
            return
        # 视图类方法映射到实体类的方法
        if self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].run_mode is not None:
            # 根据mode加载重新布局dynamic module的视图
            # 绘制标签、内容组件
            # 配置参数

            symbol_name_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].symbol_name_ls
            k_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].k_factor_ls
            vol_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].vol_factor_ls
            factor_1_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].factor_1_ls
            factor_2_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].factor_2_ls

            start_time_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].start_time_ls
            end_time_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].end_time_ls

            label_config = [
                [LabelMember.SymbolName, WidgetCategory.Combobox, symbol_name_ls, []],
                [LabelMember.KFactor, WidgetCategory.CheckButton, k_factor_ls, []],
                [LabelMember.VolFactor, WidgetCategory.CheckButton, vol_factor_ls, []],
                [LabelMember.Factor1, WidgetCategory.CheckButton, factor_1_ls, []],
                [LabelMember.Factor2, WidgetCategory.CheckButton, factor_2_ls, []],
                [LabelMember.StartTime, WidgetCategory.Entry, start_time_ls, []],
                [LabelMember.EndTime, WidgetCategory.Entry, end_time_ls, []]
            ]
            self.auto_layout.update_label_layout(label_config)
        else:
            pass

    def on_symbol_name_change(self):
        # 由实体类加载相应属性
        self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].on_symbol_name_change()
        if not self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].symbol_name:
            return
        # 视图类方法映射到实体类的方法
        if self.main_window_view.master.dynamic_module_model_dc[
            self.main_window_view.master.file_manager.now_dynamic_module].run_mode is not None:
            # 根据mode加载重新布局dynamic module的视图
            # 绘制标签、内容组件
            # 配置参数
            k_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].k_factor_ls
            vol_factor_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].vol_factor_ls
            factor_1_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].factor_1_ls
            factor_2_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].factor_2_ls

            start_time_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].start_time_ls
            end_time_ls = self.main_window_view.master.dynamic_module_model_dc[
                self.main_window_view.master.file_manager.now_dynamic_module].end_time_ls

            label_config = [
                [LabelMember.KFactor, WidgetCategory.CheckButton, k_factor_ls, []],
                [LabelMember.VolFactor, WidgetCategory.CheckButton, vol_factor_ls, []],
                [LabelMember.Factor1, WidgetCategory.CheckButton, factor_1_ls, []],
                [LabelMember.Factor2, WidgetCategory.CheckButton, factor_2_ls, []],
                [LabelMember.StartTime, WidgetCategory.Entry, start_time_ls, [0]],
                [LabelMember.EndTime, WidgetCategory.Entry, end_time_ls, [0]]
            ]

            self.auto_layout.update_label_layout(label_config)
        else:
            pass

    def layout(self):
        pass
